package com.hehe.Service;


import com.hehe.POJO.Collect;
import com.hehe.Repository.CollectRepository;
import jakarta.annotation.Resource;
import jakarta.persistence.LockModeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Optional;

@Service
public class CollectService {

    @Resource
    private CollectRepository collectRepository;

    @Transactional
    public boolean isCollect(String username, long photo_id) {
        return collectRepository.findByUsernameAndPhotoId(username, photo_id).isPresent();
    }

    public void addCollect(String username, long photo_id) {
        // 如果不存在
        if (!isCollect(username, photo_id)) {
            Collect collect = new Collect();
            collect.setUsername(username);
            collect.setPhotoId(photo_id);
            collectRepository.save(collect);
        }
    }

    public void removeCollect(String username, long photo_id) {
        collectRepository.findByUsernameAndPhotoId(username, photo_id).ifPresent(collectRepository::delete);
    }


}

